package com.fyq.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fyq.service.LoginService;
import com.fyq.util.MD5Util;

/**
 * @author 陈新雷 2015年8月6日下午5:54:13
 *
 */
@Controller
@RequestMapping("/login")
public class LoginController {
	@Autowired
	private LoginService ls;

	@RequestMapping(value = "dologin")
	public String doLogin(HttpServletRequest request) {
		String dh = "/index";
		HttpSession session = request.getSession(true);
		String userName = request.getParameter("userName") == null ? ""
				: request.getParameter("userName");
		String pwd = request.getParameter("pwd") == null ? "" : request
				.getParameter("pwd");
		Map<String, Object> param = new HashMap<String, Object>();
		if (null == session.getAttribute("user")) {
			param.put("u", userName);
			param.put("pwd", MD5Util.MD5(pwd));
			List<Map<String, Object>> tem = ls.login(param);
			if (null != tem) {
				int count = tem.size();
				if (count == 0) {
					dh = "/login";
				}else
					session.setAttribute("user", tem.get(0));
			}
		}
		return dh;
	}
}
